package com.example.permission.myjbm.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.permission.myjbm.adapter.IntegralGridAdapter;
import com.example.permission.myjbm.adapter.IntegralListAdapter;
import com.example.permission.myjbm.dao.BaseAty;
import com.example.permission.myjbm.model.IntegralModel;
import com.example.permission.myjbm.net.ReleaseNet;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jbm.jbmsupplier.util.ExProductEntity;
import com.jbm.jbmsupplier.util.IExChangeCtrl;
import com.jbm.jbmsupplier.util.IpointsCtrl;
import com.jbm.jbmsupplier.util.PointsEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 积分中心与积分明细混排
 *intent获取isMingxi    0代表积分中心  1代表积分明细
 *
 */
public class IntegralCenterAty extends BaseAty {
    //返回
    @InjectView(R.id.header_back)
    ImageView headerBack;
    //标题
    @InjectView(R.id.header_title)
    TextView headerTitle;
    //提交
    @InjectView(R.id.header_right)
    TextView headerRight;
    //积分数量
    @InjectView(R.id.integral_jifen)
    TextView integralJifen;
    //积分明细
    @InjectView(R.id.integral_mingxi)
    TextView integralMingxi;
    //兑换记录
    @InjectView(R.id.integral_record)
    TextView integralRecord;
    //积分卷
    @InjectView(R.id.integral_jifenCollect)
    GridView integralJifenCollect;
    //用于明细和积分中心切换
    @InjectView(R.id.integral_lanmu)
    LinearLayout integralLanmu;
    private IntegralModel model;
    private ReleaseNet net;
    private   PullToRefreshListView pull;
    private ImageView img_default;
    private   List<PointsEntity> data;
    private int isMing;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integral_center_aty);
        ButterKnife.inject(this);
        initView();
    }

    private void initView() {
        net= new ReleaseNet(handlerAty);
        //获取当前积分
        baseLoader.show();
        net.getTotalJifen();
            //为0代表积分中心
         isMing = getIntent().getIntExtra("isMingxi", 0);
        switch(isMing){
            case 0:
                integralLanmu.setVisibility(View.VISIBLE);
                model = new IntegralModel(headerRight, headerTitle,"积分中心", this,net);
                break;
            case 1:
                 data=new ArrayList<PointsEntity>();
                model = new IntegralModel(headerRight, headerTitle,"积分明细", this,net);
                integralLanmu.setVisibility(View.GONE);
                ViewStub stub = (ViewStub) findViewById(R.id.integral_stub);
               stub.inflate();
               pull = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
                 img_default = (ImageView) findViewById(R.id.lv_default);
                model.setPull(pull);
                break;
        }

    }
//明细适配器
    private IntegralListAdapter adapter;

    @Override
    protected void netHandler(int what,Object obj){
            switch (what){
                //获取总积分
                case ReleaseNet.JIFENOTALERROR:
                    IpointsCtrl info=(IpointsCtrl)obj;
                    integralJifen.setText(info.totoalPoints+"");
                    baseLoader.show();
                    switch(isMing){
                        case 0:
                            net.getArrowList(model.getPage());
                            break;
                        case 1:
                            net.getJifenMingxi(model.getPage());
                            break;
                    }
                    break;
                //可兑换商品列表
                case ReleaseNet.ARROWLISTERROR:
                    IExChangeCtrl info1=(IExChangeCtrl)obj;
                    List<ExProductEntity> prodectList = info1.productList;
                    integralJifenCollect.setAdapter(new IntegralGridAdapter((ArrayList) prodectList, this));
                    model.itemClick(integralJifenCollect,prodectList);
                    break;
                //积分明细
                case ReleaseNet.JIFENMINGXIERROR:
                    IpointsCtrl   info2=(IpointsCtrl)obj;
                    List<PointsEntity>  data1=info2.pointlist;
                    if(model.getPage()==0){
                        if(data1.size()==0){
                            img_default.setVisibility(View.VISIBLE);
                        }else{
                            img_default.setVisibility(View.GONE);
                        }
                        data.clear();
                        data.addAll(data1);
                         adapter = new IntegralListAdapter(data, IntegralCenterAty.this);
                        pull.setAdapter(adapter);
                    }else{
                        data.addAll(data1);
                        adapter.setNotify((ArrayList) data);
                    }

                    pull.onRefreshComplete();
                    break;
            }
    }

    @OnClick({R.id.integral_mingxi, R.id.integral_record, R.id.header_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.integral_mingxi:
                //TODO 明细页面
                Intent intent=new Intent(IntegralCenterAty.this,IntegralCenterAty.class);
                intent.putExtra("isMingxi",1);
                startActivity(intent);
                break;
            case R.id.integral_record:
                //TODO 兑换记录
                startActivity( new Intent(IntegralCenterAty.this,CertificateCenterAty.class));
                break;
            case R.id.header_back:
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode!=RESULT_OK){
            return;
        }
        baseLoader.show();
        net.getTotalJifen();

    }
}
